<template>
  <div class="app-container">
    <!-- 搜索相关区域 -->
    <div class="filter-container">
      <el-button size="mini" icon="el-icon-search" class="filter-item" @click="getPage()">{{ '搜索'
      }}</el-button>
      <el-button size="mini" icon="el-icon-plus" type="primary" class="filter-item" @click="addOrUpdateHandle()">{{ '新增'
      }}</el-button>
    </div>

    <!-- 列表相关区域 -->
    <el-table v-loading="pageLoading" :data="pageVO.list" border fit highlight-current-row style="width: 100%;">
      <!-- 角色名称 -->
      <el-table-column label="角色名称" prop="roleName" align="center">
        <template slot-scope="{row}">
          <span>{{ row.roleName }}</span>
        </template>
      </el-table-column>
      <!-- 备注 -->
      <el-table-column label="备注" prop="remark" align="center">
        <template slot-scope="{row}">
          <span>{{ row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="text" @click="addOrUpdateHandle(row.roleId)">
            {{ '编辑' }}
          </el-button>
          <el-button type="text" @click="deleteHandle(row.roleId)">
            {{ '删除' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页条 -->
    <pagination v-show="pageVO.total > 0" :total="pageVO.total" :page.sync="pageQuery.page" :limit.sync="pageQuery.size"
      @pagination="getPage()" />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getPage()" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import AddOrUpdate from './add-or-update.vue'
import * as api from '@/api/rbac/role'

export default {
  name: '',
  components: { Pagination, AddOrUpdate },
  data() {
    return {
      // 查询的参数
      pageQuery: {
        size: 10,
        page: 1
      },
      // 返回参数
      pageVO: {
        list: [], // 返回的列表
        total: 0, // 一共多少条数据
      },
      // loading
      pageLoading: true,
      // 查询参数
      searchParam: {
      },
      addOrUpdateVisible: false
    }
  },
  mounted() {
    this.getPage()
  },
  methods: {
    getPage() {
      this.pageLoading = true
      api.page({ ...this.pageQuery, ...this.searchParam }).then(({ data }) => {
        this.pageVO = data
        this.pageLoading = false
      })
    },
    addOrUpdateHandle(roleId) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(roleId)
      })
    },
    deleteHandle(roleId) {
      this.$confirm("确定进行删除操作?", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => this.deleteById(roleId))
    },
    deleteById(roleId) {
      api.deleteById(roleId).then(() => {
        this.$message({
          message: '删除成功',
          type: 'success',
          duration: 1500,
          onClose: () => this.getPage()
        })
      })
    }
  }
}
</script>
